package demo03_producerProblem;

/**
 * @author kunkun
 * @className Test_basic
 * @Description ...
 * @date 2025/3/5 17:00
 */
public class Solution_basic {

    public static void main(String[] args) {
        Problem problem = new Problem();

        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                try {
                    problem.produce();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                try {
                    problem.consume();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                try {
                    problem.consume();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"C").start();



    }



    static class Problem{

        int num=0;

        public synchronized void produce() throws InterruptedException {
            while (num==1){
                this.wait();
            }
            num++;
            System.out.println(Thread.currentThread().getName()+"--->"+num);
            this.notifyAll();
        }

        public synchronized void consume() throws InterruptedException {
            while (num==0){
                this.wait();
            }
            num--;
            System.out.println(Thread.currentThread().getName()+"--->"+num);
            this.notifyAll();
        }


    }


}
